package com.ht.action;

import java.util.Map;

import org.jbpm.api.ProcessInstance;

import com.ht.entity.Leave;
import com.ht.service.impl.JBPMServiceImpl;
import com.ht.util.BaseAction;
import com.opensymphony.xwork2.ActionContext;
/**
 * 
 * @author ht 
 * 	2010 10 20
 *
 */
public class RequestAction extends BaseAction {
	private final static String resourceName="process/leave.jpdl.xml";		
	private String taskId;
	private String leaveLong;
	private String leaveContent;
	private String leaveInstanceId;
	private JBPMServiceImpl jbpmServiceImpl = new JBPMServiceImpl();
	public String execute(){
		System.out.println("leaveContent="+leaveContent);
		if(jbpmServiceImpl.getAllPd().size()==0){	//如果还没有流程定义则发布
			jbpmServiceImpl.deployNew(resourceName);
		}
		String pdId=jbpmServiceImpl.getAllPd().get(0).getId();	//获得第一个Id
		ProcessInstance processInstance=jbpmServiceImpl.startPI((String)ActionContext.getContext().getSession().get("staffName"), pdId);		//开始流程实例
		jbpmServiceImpl.applyLeave((String)ActionContext.getContext().getSession().get("staffName"), leaveLong, leaveContent,processInstance.getId());	//添加进入数据库
		jbpmServiceImpl.completeTask(jbpmServiceImpl.getTaskId((String)ActionContext.getContext().getSession().get("staffName")),leaveLong,leaveContent);	//完成任务
		return "index";
	}
	
	//处理被驳回的请求
	public String reRequest(){
		System.out.println("taskId="+taskId);
		Map leave=(Map) jbpmServiceImpl.getLeaveDetail(taskId).get(0);
		jbpmServiceImpl.updateLeave(leave,leaveLong,leaveContent);
		jbpmServiceImpl.completeTask(taskId);
		return "index";
	}
	
	public String getLeaveContent() {
		return leaveContent;
	}
	public void setLeaveContent(String leaveContent) {
		this.leaveContent = leaveContent;
	}
	public String getLeaveLong() {
		return leaveLong;
	}
	public void setLeaveLong(String leaveLong) {
		this.leaveLong = leaveLong;
	}
	public String getLeaveInstanceId() {
		return leaveInstanceId;
	}

	public void setLeaveInstanceId(String leaveInstanceId) {
		this.leaveInstanceId = leaveInstanceId;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}
}
